package cn.edu.ctbu.dp01.service;


import cn.edu.ctbu.dp01.dao.StudentRepository;
import cn.edu.ctbu.dp01.entity.Student;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentService {


    @Autowired
    private StudentRepository studentRepository;
    /**
    *取读所有的学生表
    * @ return
     */



    public List<Student> getAll(){

        return studentRepository.findAll();

    }

    /**
     * 按ID进行查询
     * @param id 主键 整数
     * @return
     */
    public Student findById(Integer id){


        return studentRepository.findById(id).orElse(null);
    }


    /**
     * 按名字进行查询 Like
     * @param id 主键 整数
     * @return
     */
    public List<Student> findByName(String name){


        return studentRepository.findByNameLike(name);
    }


    /**
     * 按名字查询
     * @param name
     * @param password
     * @return
     */
    public List<Student> findByNameANdPaassword(String name, String password){


        return studentRepository.findByNameAndPassword(name, password);
    }

    public void insert(Student student){
        studentRepository.save(student);
    }





    public Student add(Student student){
        return studentRepository.save(student);
    }

    public Student update(Student student){
        return studentRepository.save(student);
    }

    public void delete(Integer id){
        studentRepository.deleteById(id);
    }






}
